Skip to content

Compute inverse powers of 2 instead of looking them up - #600

Merged
proost merged 1 commit into
masterfrom
remove-inv-pow2-table
Nov 23, 2026
Merged

proost merged 1 commit into
masterfrom
remove-inv-pow2-table

Conversation

@leerho

@leerho leerho commented Nov 22, 2026

Copy link
Copy Markdown
Member

HLL and CPC looked up 2^-exp in a 256-entry table of doubles (common/include/inv_pow2_table.hpp). This replaces it with inv_pow2() in common/include/inv_pow2.hpp, which builds the double from its exponent bits, as Java's Util.invPow2 does: (1023 - exp) << 52 copied into a double with std::memcpy (no std::bit_cast in C++11). It is exact for exp from 0 to 1022; current callers use at most 64.

The computed values are bit-for-bit identical to the table's for all 256 entries. The new common_test case checks every exp from 0 to 1022 against std::ldexp and std::pow. All common, HLL and CPC tests pass under ASan and UBSan.

inv_pow2_table.hpp was in the installed headers; it is replaced by inv_pow2.hpp. Code that included the old header directly would need to change, but it was an internal header.

Benchmark (Apple clang, -O3, best of 7 to 9 runs; table vs computed):

  • HLL update, lgK 12, 20M items: HLL_4 2.83-2.87 vs 2.85-2.88 ns/op; HLL_8 3.09-3.11 vs 3.18-3.19 ns/op
  • HLL with most updates changing a register (lgK 21, 20 x 1M items): 1-2% slower for HLL_4, HLL_6 and HLL_8
  • CPC update, lgK 11: 3.98-3.99 vs 3.93-3.94 ns/op
  • CPC update, serialize and deserialize: 42.2-42.7 vs 41.5-42.0 ms

🤖 Generated with Claude Code

Replace the 256-entry INVERSE_POWERS_OF_2 table with inv_pow2(), which
builds 2^-exp from the exponent bits of a double, as Java's
Util.invPow2 does. The values are bit-for-bit the same as the table's
for every index; the new test checks 0 to 1022 against ldexp and pow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 45068045751

Coverage increased (+0.001%) to 97.081%

Details

  • Coverage increased (+0.001%) from the base build.
  • Patch coverage: 3 uncovered changes across 1 file (9 of 12 lines covered, 75.0%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
hll/include/HllArray-internal.hpp 6 3 50.0%
Total (3 files) 12 9 75.0%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 17469
Covered Lines: 16959
Line Coverage: 97.08%
Coverage Strength: 1899910.85 hits per line

💛 - Coveralls

@leerho
leerho requested review from proost and tisonkun November 22, 2026 20:50
@proost
proost merged commit 4bef296 into master Nov 23, 2026
35 checks passed
@proost
proost deleted the remove-inv-pow2-table branch November 23, 2026 08:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants